/*************************************************************************  
 *   This file is part of UBBMPSREADER.
 *
 *   UBBMPSREADER is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   UBBMPSREADER is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with UBBMPSREADER.  If not, see <http://www.gnu.org/licenses/>. 
 *************************************************************************/

package cs.ubbcluj.ro.mps.states;

import java.io.IOException;
import java.io.PushbackInputStream;

import cs.ubbcluj.ro.mps.data.BoundsType;
import cs.ubbcluj.ro.mps.data.interfaces.Mps;
import cs.ubbcluj.ro.mps.reader.MpsInputStream;
import cs.ubbcluj.ro.mps.states.interfaces.State;
import cs.ubbcluj.ro.mps.states.utils.CharConstants;

/**
 * Represents the BOUNDS section of the MPS file
 * 
 * @author avadas
 * @version 1.0
 */
public strictfp class BoundsState extends AbstractState {

    /** True if we parsed the whole section */
    private boolean endOfSection;

    /** True if file has ranges section */
    private boolean hasRanges;

    /** The ranges state */
    private final State rangesState;

    public BoundsState(final State nextState, final State errorState,
	    final State rangesState) {
	super(nextState, errorState);
	this.rangesState = rangesState;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public State nextState(final MpsInputStream input, final Mps mps)
	    throws IOException {

	if (consumeFirstColumn(input)) {
	    return this.errorState;
	}

	while (!this.endOfSection) {

	    final StringBuffer boundType = new StringBuffer();
	    consumeData(input, boundType);

	    consumeSpaces(input);

	    final StringBuffer boundName = new StringBuffer();
	    consumeData(input, boundName);

	    consumeSpaces(input);

	    final StringBuffer variableName = new StringBuffer();
	    consumeData(input, variableName);

	    final StringBuffer boundValue = new StringBuffer();
	    try {
		if (BoundsType.valueOf(boundType.toString()) != BoundsType.FR
			&& BoundsType.valueOf(boundType.toString()) != BoundsType.PL
			&& BoundsType.valueOf(boundType.toString()) != BoundsType.MI) {
		    consumeSpaces(input);
		    consumeData(input, boundValue);
		} else {
		    boundValue.append("0");
		}
	    } catch (final IllegalArgumentException e) {
		this.errorState.nextState(input, mps);
	    }

	    try {
		mps.addVariableBound(boundName.toString(), variableName
			.toString(), BoundsType.valueOf(boundType.toString()),
			new Double(boundValue.toString()));
	    } catch (final NumberFormatException exception) {
		return this.errorState;
	    }

	    if (input.read() == CharConstants.CR) {
		input.read();
	    }

	    if (consumeFirstColumn(input)) {
		return this.errorState;
	    }
	}
	return (this.hasRanges) ? this.rangesState : this.nextState;
    }

    /**
     * 
     * @param input
     *            the inputstream
     * @return true if was a parseError
     * @throws IOException
     *             see {@link IOException}
     */
    private boolean consumeFirstColumn(final PushbackInputStream input)
	    throws IOException {
	final int in = input.read();
	if (in != 32) {
	    if (((char) in == 'E') || ((char) in == 'R')) {
		this.endOfSection = true;
		input.unread(in);
		if (in == 'R') {
		    this.hasRanges = true;
		}
		return false;
	    } else {
		return true;
	    }
	}
	return false;
    }
}
